package com.buka.book.controller;

import com.buka.book.entity.Book;
import com.buka.book.service.BookService;
import com.buka.common.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/add")
public class AddServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookName = req.getParameter("bookName");
        String bookNo = req.getParameter("bookNo");
        Integer count = Integer.parseInt(req.getParameter("count"));

        //新增是没有id ,id 后台生成的或者是数据库自动递增的
        Book book = new Book(bookNo, bookName, count);

        int i = new BookService().save(book);

        if (i > 0) {
            resp.getWriter().write(Result.success(null));
        }else{
            resp.getWriter().write(Result.error("保存失败"));
        }

    }
}
